import QtQuick 2.0
import QtQuick.Window 2.1
import FileIO 1.0
//import "ops.js" as Ops

Image {
    id: dialogComponent
    source: "LIFT/ui2/fixedbkgnd.png"

        property string rotateLeft: "\u2939"
        property string rotateRight: "\u2935"
        property string leftArrow: "\u2190"
        property string upperArrow: "\u2191"
        property string division : "\u00f7"
        property string multiplication : "\u00d7"
        property string squareRoot : "\u221a"
        property string plusminus : "\u00b1"
        property var xmldata
        property var jsonO
        property int defvol

        property string ddd1;
        signal logout;
        property bool laug:true
        property bool laugok:true

         property bool voiceok

        property bool arr1flag

         property string visions

        property var arr1:["主操纵厢","后操纵厢","残疾人操纵厢","副操纵厢"];
        //property var arr4:["不播放","仅播报提示信息语音","仅播报语音报站","播报提示信息语音和语音报站"];
        property var arr4:["不播报","语音报站"];
        property var arr1_en:["main operation cabinet","rear operation cabinet","the disabled operation cabinet","auxiliary operation cabinet"];
        //property var arr4_en:["no broadcast","only prompt message broadcast","only voice broadcast","prompt message and voice broadcast"];
        property var arr4_en:["no broadcast"," voice broadcast"];
        property var arr5:["English","中文"];

       //onDefvolChanged: vk2.setvol(defvol)

        // if the background is clicked then other Items lose focus
        MouseArea {
                x:0
                y:0
                width: 768
                height: 1024
                onClicked: { parent.focus = true; vk1.state = "inactive";  vk2.state = "inactive";  vk3.state = "inactive";
                    vk4.state = "inactive";  vk5.state = "inactive"; vk6.state = "inactive"; vk.state = "inactive"; }

        }

        FileIO {
               id: io
               source: "liftcfg.json"
        }

        Column {
            anchors.topMargin: 10
               spacing: 6
               anchors { fill: parent; margins: 8; }

               Row{
               spacing: 8
               Buttontmp { id:vision; width: 260;text: laug ? "软件版本: MxV1.0":"Software: MxV1.0";db:0}
               Buttontmp { id:vision1; width: 260;text: visions ;db:0}
               }

               Row{
               spacing: 8
                Buttontmp { width: 260;text: laug ? "操纵厢类型:":"Car Mode:" ;db:0}
               LineEdit {
                   id: lineEdit1
                   width: 260; height: 50
                   MouseArea {
                       anchors.fill: parent
                       onClicked: { vk1.state = "active";  vk2.state = "inactive";  vk3.state = "inactive";
                           vk4.state = "inactive";  vk5.state = "inactive";  vk6.state = "inactive"; vk.state = "inactive"; parent.focus = true }
                   }
               }
               }

               Row{
                   spacing: 8
                Buttontmp { width: 260;text: laug ? "语音音量:":"Volume:";db:0}
               LineEdit {
                          id: lineEdit2
                          width: 260; height: 50
                          MouseArea {
                              anchors.fill: parent
                              onClicked: {
                                  vk1.state = "inactive";  vk2.state = "active";  vk3.state = "inactive";
                                                             vk4.state = "inactive";  vk5.state = "inactive"; vk6.state = "inactive";  vk.state = "inactive";
                                  parent.focus = true
                              }
                          }
                      }
                }
               Row{
                   spacing: 8
                Buttontmp { width: 260;text:laug ? "按键等待时间:":"Key Interval Time:";db:0}
              LineEdit {
                  id: lineEdit3
                  width: 260; height: 50
                  MouseArea {
                      anchors.fill: parent
                      onClicked: { vk1.state = "inactive";  vk2.state = "inactive";  vk3.state = "active";
                          vk4.state = "inactive";  vk5.state = "inactive"; vk6.state = "inactive";   vk.state = "inactive"; parent.focus = true }
                  }
              }
               }

               Row{
                   spacing: 8
                Buttontmp { width: 260;text:laug ? "语音播报:":"Voice Broadcast:";db:0}
              LineEdit {
                         id: lineEdit4
                         width: 260; height: 50
                         MouseArea {
                             anchors.fill: parent
                             onClicked: {
                                 vk1.state = "inactive";  vk2.state = "inactive";  vk3.state = "inactive";
                                                            vk4.state = "active";  vk5.state = "inactive";  vk6.state = "inactive";  vk.state = "inactive";
                                 parent.focus = true
                             }
                         }
                     }
            }
               Row{
                   spacing: 8
                Buttontmp { width: 260;text:laug ? "语言/Language:":"Language/语言:";db:0}
             LineEdit {
                 id: lineEdit5
                 width: 260; height: 50
                 MouseArea {
                     anchors.fill: parent
                     onClicked: { vk1.state = "inactive";  vk2.state = "inactive";  vk3.state = "inactive";
                         vk4.state = "inactive";  vk5.state = "active";  vk6.state = "inactive";  vk.state = "inactive";parent.focus = true }
                 }
             }
               }
               Row{
                   spacing: 8
                    Buttontmp { width: 260;text: laug ? "左下特殊按键:":"Left Special Key:";db:0}
                    LineEdit {
                    id: lineEdit6
                    width: 260; height: 50
                    MouseArea {
                        anchors.fill: parent
                        onClicked: {
                        vk1.state = "inactive";  vk2.state = "inactive";  vk3.state = "inactive";
                                                   vk4.state = "inactive";  vk5.state = "inactive";  vk6.state = "inactive";  vk.state = "active";
                        parent.focus = true
                        }
                    }
                }
            }

               Row{
                   spacing: 8
                    Buttontmp { width: 260;text: laug ? "参数复位:":"Parameter Reset:";db:0}
                    LineEdit {
                    id: lineEdit7
                    width: 260; height: 50
                    MouseArea {
                        anchors.fill: parent
                        onClicked: {
                        vk1.state = "inactive";  vk2.state = "inactive";  vk3.state = "inactive";
                                                   vk4.state = "inactive";  vk5.state = "inactive";  vk6.state = "active";  vk.state = "inactive";
                        parent.focus = true
                        }
                    }
                }
            }

               Row{
                   spacing: 8
                   Buttontmp { width: 260;text:laug ?  "保存":"Save";Component.onCompleted: clicked.connect(buttonPressed1)}
                   Buttontmp { width: 260;text: laug ? "退出":"Quit";Component.onCompleted: clicked.connect(buttonPressed)}
                }
     }


    VirtualKeyboard {
           id: vk;
           onButtonPressed: {
               console.log("Keyboard sent: " + op)
               //Ops.doOperation(op)
               lineEdit6.text = op
               xmldata.k06 = lineEdit6.text
           }
       }


    VirtualKeyboard1 {
           id: vk1;
           onButtonPressed: {
               console.log("Keyboard sent: " + op)
               //Ops.doOperation(op)
               lineEdit1.text = laug?arr1[parseInt(op,10)]:arr1_en[parseInt(op,10)]
               xmldata.k01 = op
               arr1flag = true
           }
       }
    VirtualKeyboard2 {
           id: vk2;
           onButtonPressed: {
               console.log("Keyboard sent: " + op)
               //Ops.doOperation(op)
               lineEdit2.text = op
               xmldata.k02 = lineEdit2.text
           }
       }
    VirtualKeyboard3 {
           id: vk3;
           onButtonPressed: {
               console.log("Keyboard sent: " + op)
               //Ops.doOperation(op)
               lineEdit3.text = op
               xmldata.k03 = lineEdit3.text
           }
       }
    VirtualKeyboard4 {
           id: vk4;
           onButtonPressed: {
               console.log("Keyboard sent: " + op)
               //Ops.doOperation(op)
               lineEdit4.text = laug?arr4[parseInt(op,10)]:arr4_en[parseInt(op,10)]
               xmldata.k04 = op
           }
       }
    VirtualKeyboard5 {
           id: vk5;
           onButtonPressed: {
               console.log("Keyboard sent: " + op)
               //Ops.doOperation(op)
               lineEdit5.text = arr5[parseInt(op,10)]
               xmldata.k05 = op
               if(xmldata.k05 == "1")
                     laug = true
               else
                   laug = false
               //timer.start()

              lineEdit1.text=laug?arr1[parseInt(xmldata.k01,10)]:arr1_en[parseInt(xmldata.k01,10)]
              lineEdit4.text=laug?arr4[parseInt(xmldata.k04,10)]:arr4_en[parseInt(xmldata.k04,10)]

           }
       }

    VirtualKeyboard6 {
           id: vk6;
           onButtonPressed: {
               console.log("Keyboard sent: " + op)
               //Ops.doOperation(op)
               var t = lineEdit7.text
               if (op == leftArrow) {
                       t = t.toString().slice(0, -1)
                       if (t.length == 0)
                           t = ""
               }
               else
                   t=t+op
               lineEdit7.text = t

               if(t.length>=6)
               {
                   if(t=="753210")
                   {
                       timer1.start()
                   }
                   else
                      lineEdit7.text = ""
               }
           }
       }

    Timer {
       id: timer1;
       interval: 500;
       running: false
       repeat: false;
       triggeredOnStart: false;
       onTriggered:{
           laug = true
           lineEdit1.text = "主操纵厢"
           xmldata.k01 = "0"
           lineEdit2.text = "8"
           xmldata.k02 = lineEdit2.text
           lineEdit3.text = "1S"
           xmldata.k03 = lineEdit3.text
           lineEdit4.text = "不播放"
           xmldata.k04 = "0"
           lineEdit5.text = "中文"
           xmldata.k05 = "1"
           lineEdit6.text = "L"
           xmldata.k06 = lineEdit6.text
           lineEdit7.text = ""

           defvol = parseInt(xmldata.k02);
           vk2.setvol(defvol)
       }
    }

    Timer {
       id: timer;
       interval: 1;
       running: false
       repeat: false;
       triggeredOnStart: false;
       onTriggered:{
//             lineEdit6.text=xmlModel.get(0).title
//            lineEdit1.text=xmlModel.get(1).title
//            lineEdit2.text=xmlModel.get(2).title
//            lineEdit3.text=xmlModel.get(3).title
//            lineEdit4.text=xmlModel.get(4).title
//            lineEdit5.text=xmlModel.get(5).title

           if(xmldata.k05 == "1")
           {
                 laug = true
           }
           else
           {
               laug = false
           }

           lineEdit6.text=xmldata.k06
          lineEdit1.text=laug?arr1[parseInt(xmldata.k01,10)]:arr1_en[parseInt(xmldata.k01,10)]
          lineEdit2.text=xmldata.k02
          lineEdit3.text=xmldata.k03
          lineEdit4.text=laug?arr4[parseInt(xmldata.k04,10)]:arr4_en[parseInt(xmldata.k04,10)]
          lineEdit5.text=arr5[parseInt(xmldata.k05,10)]

         defvol = parseInt(xmldata.k02);
         vk2.setvol(defvol)

           if(laug)
               laugok=true
           else
               laugok=false

           if(parseInt(xmldata.k04,10)==0)
                voiceok=false
           else
               voiceok=true

           sfu.keypress(17)
       }
   }

    function readDocument() {
          io.read()
          xmldata= JSON.parse(io.text)
//          xmldata  = eval("("+io.text+")");
        sfu.keypress(parseInt(xmldata.k01,10)+18)

      }

      function saveDocument() {
          var data = xmldata
          io.text = JSON.stringify(data)
          io.write()

          if(arr1flag)
          {
            sfu.keypress(parseInt(xmldata.k01,10)+18)
            arr1flag = false
          }

          if(laug)
              laugok=true
          else
              laugok=false

          if(parseInt(xmldata.k04,10)==0)
               voiceok=false
          else
              voiceok=true

          defvol = parseInt(xmldata.k02);
          vk2.setvol(defvol)
      }

      function buttonPressed1()
      {
              // destroy object is needed when you dynamically create it
              vk1.state = "inactive";  vk2.state = "inactive";  vk3.state = "inactive"; vk4.state = "inactive";
              vk5.state = "inactive"; vk6.state = "inactive"; vk.state = "inactive";parent.focus = true;
              saveDocument()
      }

      function buttonPressed()
      {
              // destroy object is needed when you dynamically create it
              vk1.state = "inactive";  vk2.state = "inactive";  vk3.state = "inactive"; vk4.state = "inactive";
              vk5.state = "inactive"; vk6.state = "inactive"; vk.state = "inactive";parent.focus = true;
              logout()
              //dialogComponent.destroy()
      }

      function  loadcfg() {
          arr1flag = false
          readDocument()
          timer.start()

      }

      Component.onCompleted: {
          loadcfg()
      }
}
